Repository navigation
[GH-3178] Add Geography support for ST_KNN as a broadcast join - #3542
Merged
Merged
Conversation
jiayuasu
marked this pull request as draft
November 12, 2026 08:48
jiayuasu
force-pushed
the
feature/geography-knn-join
branch
from
November 13, 2026 07:04
c621149 to
2efd59f
Compare
jiayuasu
force-pushed
the
feature/geography-knn-join
branch
4 times, most recently
from
November 17, 2026 07:43
fba85cf to
4f704de
Compare
ST_KNN(geography, geography, k) joins each query row with its k nearest object rows, ranked by the distance that ST_Distance returns for two geographies: the minimum distance on the sphere, in meters. Only the 3-argument form accepts geographies, because spherical ranking is the definition for geography rather than an option. The join is planned as BroadcastGeographyKNNJoinExec with either side broadcast. Geographies are indexed by their latitude / longitude bounding rectangle, and a best-first search prunes by the distance on the sphere between rectangles, so neighbours are found correctly across the antimeridian and around the poles. ST_KNN cannot be evaluated one pair at a time, so a geography KNN join that has no broadcast side, or is not an inner join, is rejected at planning time with an error that says what to change. A partitioned geography KNN join is left for a follow-up.
jiayuasu
force-pushed
the
feature/geography-knn-join
branch
from
November 17, 2026 08:56
4f704de to
0e316f8
Compare
jiayuasu
marked this pull request as ready for review
November 17, 2026 08:56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Did you read the Contributor Guide?
Is this PR related to a ticket?
[GH-XXX] my subject. Closes Add Geography support for ST_KNN #3178What changes were proposed in this PR?
ST_KNN(geography, geography, k)joins each query row with itsknearest object rows. Neighbours are ranked by the distance thatST_Distancereturns for two geographies: the minimum distance on the sphere, in meters. Only the 3-argument form accepts geographies. The 4-argument form stays geometry-only, and mixed geometry / geography arguments are rejected at analysis time.This PR covers the broadcast join, on top of the index added in #3543. A partitioned geography KNN join is left for a follow-up.
ST_KNNgets a(Geography, Geography, Int)overload.JoinQueryDetectorplans geography KNN as a newBroadcastGeographyKNNJoinExec. Either side can be broadcast, by hint or bysedona.join.autoBroadcastJoinThreshold.knearest.GeographyKnnIndexfrom [GH-3178] Add a k-nearest-neighbour index for Geography #3543, a best-first search over the latitude / longitude bounding rectangles of the geographies that prunes with the distance on the sphere between rectangles. This keeps the result correct across the antimeridian and around the poles, where a planar bound in degrees is not a lower bound.JoinedGeometry.geographyToEnvelopeGeometry, used by the other geography joins, now takes its envelope fromGeographyKnnIndex.toEnvelopeinstead of repeating the same logic.ST_KNNcannot be evaluated one pair at a time, so there is no row-by-row fallback. A geography KNN join with no broadcast side, or with a join type other than inner, fails at planning time with an error that says what to change.spark.sedona.join.knn.includeTieBreakers, and residualONpredicates behave as they do for the geometry KNN join.How was this patch tested?
New
KnnJoinGeographySuiteruns every result test in four layouts (object or query side broadcast, queries or objects first in the join):ST_Distanceklarger than the object count, NULL and empty inputs, ties with and without tie breakers, residual conditions, the DataFrame API, empty sidesNew
BroadcastGeographyKnnIndexSuitecovers how rows travel between the driver and the executors: the broadcast index and the shuffled neighbour lists are used after Java and Kryo serialization. The search itself is tested in #3543.Run locally together with the existing
KnnJoinSuite,BroadcastIndexJoinGeographySuite,LegacyGeographyDistanceJoinSuite,BroadcastIndexJoinSuite,predicateJoinTestScalaandpredicateTestScalaon Spark 3.5 / Scala 2.12, and withKnnJoinSuiteand the geography suites on Spark 4.2 / Scala 2.13.Did this PR include necessary documentation updates?
vX.Y.Zformat.